package com.zznzzn.myweb.service;

import com.zznzzn.myweb.exceptions.ServiceException;
import com.zznzzn.myweb.mapper.ChatMapper;
import com.zznzzn.myweb.pojo.Record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RecordServiceImpl implements RecordService{
    @Autowired
    private ChatMapper chatMapper;

    @Override
    public int newRecord(Record record) {
        int i = chatMapper.addChatRec(record);
        if (i!=0){
            return i;
        }else{
            throw new ServiceException("插入聊天记录失败");
        }
    }

    @Override
    public List<Record> queryRecordsBetweenUuidBylimit(String fromid, String toid, int limit) {
        List<Record> records = chatMapper.queryRecordsBetweenUuidBylimit(fromid, toid, limit);
        if (records!= null){
            return records;
        }else{
            throw new ServiceException("没有找到聊天记录");
        }
    }

    @Override
    public List<Record> queryRecordsBetweenUuidByAfter(String fromid, String toid, Date time) {
        List<Record> records = chatMapper.queryRecordsBetweenUuidAfterTime(fromid, toid, time);
        if (records!= null){
            return records;
        }else{
            throw new ServiceException("没有找到聊天记录");
        }
    }

    @Override
    public List<Record> queryRecordsBetweenUuid(String fromid, String toid) {
        List<Record> records = chatMapper.queryRecordsBetweenUuid(fromid, toid);
        if (records!= null){
            return records;
        }else{
            throw new ServiceException("没有找到聊天记录");
        }
    }

    @Override
    public List<Record> queryRecordsBetweenUuidAfterTimeByLimit(String fromid, String toid,Date time,int limit) {
        List<Record> records = chatMapper.queryRecordsBetweenUuidAfterTimeByLimit(fromid, toid,time,limit);
        if (records!= null){
            return records;
        }else{
            throw new ServiceException("没有找到聊天记录");
        }
    }

    @Override
    public List<Record> queryRecordsOfUuid(String fromid, int limit) {
        List<Record> records = chatMapper.queryRecordsOfUuid(fromid, limit);
        if (records!= null){
            return records;
        }else{
            throw new ServiceException("没有找到聊天记录");
        }
    }

    @Override
    public int setReceived(String fromid, String toid) {
        return chatMapper.setReceived(fromid, toid);
    }
}
